package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.eotms.request.FlightOrderRequestDto;
import cn.buk.tms.eotms.response.FlightOrderResponseDto;
import cn.buk.tms.eotms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.eotms.service.FlightOrderService;
import cn.buk.tms.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/**
 * @author yfdai
 */
@RestController
@RequestMapping("/flight")
public class  FlightOrderController extends BaseController {

  @Autowired
  public FlightOrderController(FlightOrderService flightOrderService) {
    this.flightOrderService = flightOrderService;
  }

  private final FlightOrderService flightOrderService;

  /**
   * 查找机票订单
   */
  @RequestMapping("/orders")
  public CommonDto<FlightOrderResponseDto> searchFlightOrders(HttpServletRequest request,
                                                   @ModelAttribute("sc") SearchFlightOrderCriteria sc) {
    return flightOrderService.searchFlightOrders2(getUserId(request), sc);
  }

  /**
   * 获取机票订单详情
   */
  @GetMapping(value = "/order/{id}")
  public FlightOrderResponseDto getFlightOrderDetail(HttpServletRequest request,
                                                     @PathVariable("id") int id) throws BaseException {
    return flightOrderService.getFlightOrderDetail(getUserId(request), id);
  }

  /**
   * 获取机票订单详情
   */
  @RequestMapping("/order/orderNo/{orderNo}")
  public FlightOrderResponseDto getFlightOrderDetailByOrderNo(HttpServletRequest request,
                                                              @PathVariable("orderNo") String orderNo) throws BaseException {
    return flightOrderService.getFlightOrderDetailByOrderNo(getUserId(request), orderNo);
  }

  /**
   * 创建机票订单
   */
  @PostMapping(value = "/order", consumes = "application/json")
  public JsonResult createFlightOrder(HttpServletRequest request,
                                                     @RequestBody FlightOrderRequestDto dto) throws BaseException {
    return flightOrderService.createFlightOrder(getUserId(request), dto);
  }

  /**
   * 为机票订单付款
   */
  @PostMapping(value = "/order/{id}/pay")
  public JsonResult payFlightOrder(HttpServletRequest request,
                                         @PathVariable("id") int id) throws BaseException {
    return flightOrderService.payFlightOrder(getUserId(request), id);
  }

  /**
   * 申请开票
   */
  @PostMapping(value = "/order/{id}/ticketing")
  public JsonResult submitFlightOrder(HttpServletRequest request,
                                      @PathVariable("id") int orderId) {
    return flightOrderService.submitFlightOrder(getUserId(request), orderId);
  }

  /**
   * 审批订单
   * @param denyCode 0-同意；其它-不同意
   * @param denyReason 不同意的原因，不同意时必填
   */
  @PostMapping(value = "/order/{id}/approve")
  public JsonResult approveFlightOrder(HttpServletRequest request,
                                       @PathVariable("id") int orderId,
                                       @RequestParam("denyCode") int denyCode,
                                       @RequestParam(value = "denyReason", required = false) String denyReason) {
    return flightOrderService.approveFlightOrder(getUserId(request), orderId, denyCode, denyReason);
  }

}
